<?php
class SignupForm extends CFormModel {
	// From table tbl_user
	public $id;
	public $username = "";
	public $email = "";
	public $re_password = "";
	public $password = "";
	public $phone = "";
	public $verifyCode = "";

	private $_is_exists = false;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__){
		return parent::model($className);
	}

	public function __construct($id = null){
		if(!empty($id)){
			$user = User::model()->findByPk	($id);
			if(is_object($user)){
				$this->_is_exists = true;
				$this->setAttributes($user->getAttributes(), false);
				$this->id = $user->id;
				$this->password = "";
			}
		}
	}

	public function isExists(){
		return $this->_is_exists;
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username', 'required', 'on' => 'insert', 'message' => Yii::t('msg', '0004')),
			array('email', 'required', 'on' => 'insert', 'message' => Yii::t('msg', '0005')),
			array('password', 'required', 'on' => 'insert', 'message' => Yii::t('msg', '0006')),
			array('username, email', 'length', 'max'=>200),
			array('password', 'length', 'max'=>128 , 'min' => 6),
			array('re_password', 'compare','compareAttribute' => 'password', 'message' => Yii::t('msg', '0007')),
			array('email','email'),
			array('username', 'checkExists','on' => 'insert'),
			array('email', 'checkExists'),
			array('phone','checkExistPhone'),
			array('verifyCode', 'application.validators.CaptchaAjax'),
		);
	}

	public function checkExists($attribute, $params){
		if($this->scenario == "update"){
			$user = User::model()->findByPk($this->id);
			if(is_object($user) && $user->email == $this->$attribute){
				return true;
			}
		}
		$count = User::model()->countByAttributes(array($attribute =>$this->$attribute));
        if($count == 1){
        	$msg = ($attribute == "email") ? Yii::t('msg', '0010') : Yii::t('msg', '0011');  
        	$this->addError($attribute, $msg);
        	return false;
        }
        return true;
    }

    public function checkExistPhone($attribute, $params){
		if(empty($this->$attribute)){
			return true;
		}
		if($this->scenario == "update"){
			$userProfile = UserProfile::model()->findByPk($this->id);
			if(is_object($userProfile) && $userProfile->phone == $this->$attribute){
				return true;
			}
		}
		$count = UserProfile::model()->countByAttributes(array($attribute =>$this->$attribute));
        if($count == 1){
        	$msg = Yii::t('msg', '0029');
        	$this->addError($attribute, $msg);
        	return false;
        }
        return true;		
    }

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels(){
		return array(
			'id' => 'ID',
			'username' => Yii::t('lbl' , '0003'),
			'email' => Yii::t('lbl' , '0006'),
			'password' => Yii::t('lbl' , '0004'),
			're_password' => Yii::t('lbl' , '0005'),
			'phone' => Yii::t('lbl' , '0007'),
			'verifyCode' => Yii::t('lbl' , '0008'),
		);
	}

	public function afterSave(){

		return parent::afterSave();
	}


}